﻿using System;
using System.Drawing;
using System.Windows.Forms;
using TimeLineControlNamedPipes;

namespace TimeLineControl
{
    public partial class FrmConnectPowerPoint : Form
    {
        private bool _validApp;

        public FrmConnectPowerPoint()
        {
            InitializeComponent();
        }

        private void TimerTick(object sender, EventArgs e)
        {
            progressBar.ForeColor = Color.Blue;
            progressBar.Value += 1;
            try
            {
                //TRY CONNECT TO POWERPOINT (SEND PIPE NAME)
                SenderPipe.SendPowerPointPipeOk(DataConfigTimeLine.PowerPointPipeName);

                //SEND TO POWERPOINT STATUS TIMELINE BAR
                SenderPipe.SendPowerPointShowBar(DataConfigTimeLine.TimeLineBarVisibled, DataConfigTimeLine.PowerPointPipeName);

                LblStatus.Text = ScreenUtil.StringResource("PptconectOK") + DataConfigTimeLine.PowerPointPipeName;
                progressBar.ForeColor = Color.Green;
                progressBar.Value = progressBar.Maximum;
                timer.Enabled = false;
                _validApp = true;
                System.Threading.Thread.Sleep(1000);
                Close();
            }
            catch (Exception)
            {
                _validApp = false;
                if (progressBar.Value == progressBar.Maximum)
                {
                    progressBar.ForeColor = Color.Red;
                    timer.Enabled = false;
                    LblStatus.ForeColor = Color.Red;
                    LblStatus.Text = string.Format(ScreenUtil.StringResource("PptconectNOK"), DataConfigTimeLine.PowerPointPipeName);
                }
            }
        }

        private void FrmConnectPowerPoint_Load(object sender, EventArgs e)
        {
            numericUpDownPolling.Value = DataConfigTimeLine.PowerPointPolling;
            LblStatus.Text = "";
        }

        private void BntConnect_Click(object sender, EventArgs e)
        {
            progressBar.Value = 0;
            LblStatus.ForeColor = Color.Black;
            LblStatus.Text = ScreenUtil.StringResource("TryConnect") + DataConfigTimeLine.PowerPointPipeName;
            timer.Enabled = true;
        }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FrmConnectPowerPoint_FormClosing(object sender, FormClosingEventArgs e)
        {
            DataConfigTimeLine.PowerPointConnected = _validApp;
            DataConfigTimeLine.PowerPointPolling = (int)numericUpDownPolling.Value;
        }
    }
}
